var base64 = require("../weui/example/images/base64");
//获取应用实例
const app = getApp()

Page({
  data: {
    userId: null,
    subjectArr: [],
    testArr: [],
    displayType: 0,
    display: false,
    marginLeft: 800,
    pageNo: 1,
    pageSize: 20,
    result:[],
    right: [],
    rightStr: [],
    score: '',
    lookMore: '数据加载中...',
    wh:0
  },
  onShow: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          "wh": res.windowHeight - 48 -wx.getSystemInfoSync().statusBarHeight
        })
      }
    })
  },
  goBack: function(){
    wx.navigateBack({
      delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
    });
  },
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: 'https://www.xinhuo.site/api/subject/list',
      data: {
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        if (res.data.syscode == 0) {
          var subArr = res.data.recordList;
          that.data.subjectArr = subArr;
          that.setData({
            subjectArr: that.data.subjectArr
          })
          for (var i = 0; i < subArr.length; i++) {
            var rightItem = [];
            for (var j = 0; j < subArr[i].answerList.length; j++) {
              if (subArr[i].answerList[j].isRight == 1) {
                rightItem.push(subArr[i].answerList[j].optionSort);
              }
            }
            that.data.right[i] = rightItem;
            that.data.rightStr[i] = rightItem.join('');
          }
          that.setData({
            right: that.data.right,
            rightStr: that.data.rightStr
          });
        }
        else {
          
        }
      }
    })
    wx.request({
      url: 'https://www.xinhuo.site/api/dailytest/list',
      data: {
        pageNo: this.data.pageNo,
        pageSize: this.data.pageSize,
        userId: app.globalData.userId
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        if (res.data.syscode == 0) {
          if (res.data.recordList.length == that.data.pageSize) {
            that.data.lookMore = '-上滑查看更多-'
          }
          else {
            that.data.lookMore = '-没有更多内容-'
          }
          that.data.testArr = that.data.testArr.concat(res.data.recordList)
          that.setData({
            testArr: that.data.testArr,
            lookMore: that.data.lookMore
          })
        }
        else {
          that.setData({
            lookMore: '-暂无测试记录-'
          })
        }
      }
    })
  },
  selectTab1: function (e) {
    this.setData({
      displayType: 0
    });
  },
  selectTab2: function (e) {
    this.setData({
      displayType: 1
    });
  },
  checkboxChange(e) {
    var select = e.detail.value;
    var n = e.target.dataset.n;
    var res = this.data.result;
    res[n] = select;
    console.log(res);
  },
  tosave: function(){
    var that = this;
    var score = 0;
    var subjectArr = this.data.subjectArr;
    for (var i = 0; i < subjectArr.length; i++){
      var isRight = true;
      if (this.data.right[i].length == this.data.result[i].length) {
        for (var j = 0; j < this.data.result[i].length; j++) {
          if (this.data.rightStr[i].indexOf(this.data.result[i][j]) >= 0) {
            continue;
          }
          else {
            isRight = false;
          }
        }
      }
      else {
        isRight = false;
      }
      if (isRight == true) {
        score += 10;
      }
    }
    this.setData({
      display: true,
      score: score
    })
    wx.showToast({
      title: '您的得分为' + this.data.score + '分',
      icon: 'none',
      duration: 2000
    })
    wx.request({
      url: 'https://www.xinhuo.site/api/dailytest/save',
      data: {
        userId: app.globalData.userId,
        score: this.data.score
      },
      method: 'POST',
      header: {
        'Content-Type': 'application/json'
      },
      success(res) {
        if (res.data.syscode == 0) {
          that.setData({
            pageNo: 1
          });
          wx.request({
            url: 'https://www.xinhuo.site/api/dailytest/list',
            data: {
              pageNo: that.data.pageNo,
              pageSize: that.data.pageSize,
              userId: app.globalData.userId
            },
            method: 'POST',
            header: {
              'Content-Type': 'application/json'
            },
            success(res) {
              if (res.data.syscode == 0) {
                if (res.data.recordList.length == that.data.pageSize) {
                  that.data.lookMore = '-上滑查看更多-'
                }
                else {
                  that.data.lookMore = '-没有更多内容-'
                }
                that.data.testArr = res.data.recordList
                that.setData({
                  testArr: that.data.testArr,
                  lookMore: that.data.lookMore
                })
              }
              else {
                that.setData({
                  lookMore: '-暂无测试记录-'
                })
              }
            }
          })
        }
        else {
        }
      }
    })
  },
  onReachBottom() {
    if (this.data.displayType == 1) {
      var that = this;
      this.setData({
        pageNo: this.data.pageNo + 1
      });
      wx.request({
        url: 'https://www.xinhuo.site/api/dailytest/list',
        data: {
          pageNo: this.data.pageNo,
          pageSize: this.data.pageSize,
          userId: app.globalData.userId
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success(res) {
          if (res.data.syscode == 0) {
            if (res.data.recordList.length == that.data.pageSize) {
              that.data.lookMore = '-上滑查看更多-'
            }
            else {
              that.data.lookMore = '-没有更多内容-'
            }
            that.data.testArr = that.data.testArr.concat(res.data.recordList)
            that.setData({
              testArr: that.data.testArr,
              lookMore: that.data.lookMore
            })
          }
          else {
            that.setData({
              lookMore: '-没有更多内容-'
            })
          }
        }
      })
    }
  }
})
